package com.burstly.lib.network.beans.cookie;

import android.content.Context;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public final class CookieManager
{
  static final CountDownLatch LATCH = new CountDownLatch(1);
  static final LoggerExt LOG = LoggerExt.getInstance();
  private static final Executor SAVE_QUEUE = Executors.newSingleThreadExecutor(new ThreadFactory()
  {
    public Thread newThread(Runnable paramAnonymousRunnable)
    {
      Thread localThread = new Thread(paramAnonymousRunnable);
      localThread.setName("db save cookies thread");
      return localThread;
    }
  });
  static final String TAG = "CookieManager";
  static volatile boolean sIsInitStarted;
  static volatile boolean sIsReady;
  static ICookieStorage sMemoryCookieStorage;
  static ICookieStorage sSQLiteCookieStorage;

  private static void checkManager()
  {
    if (!sIsInitStarted)
      throw new RuntimeException("Can not use CookieManager before it was initialized. Call CookieManager.initCookieManager() method first!");
  }

  static void destroy()
  {
    try
    {
      checkManager();
      waitForReadyState();
      sIsInitStarted = false;
      sIsReady = false;
      sSQLiteCookieStorage = null;
      sMemoryCookieStorage = null;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public static ICookieStorage getMemoryCookieStorage()
  {
    try
    {
      checkManager();
      waitForReadyState();
      ICookieStorage localICookieStorage = sMemoryCookieStorage;
      return localICookieStorage;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public static ICookieStorage getSQLiteCookieStorage()
  {
    try
    {
      checkManager();
      waitForReadyState();
      ICookieStorage localICookieStorage = sSQLiteCookieStorage;
      return localICookieStorage;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public static CookieHolder[] getValidCookies()
  {
    checkManager();
    waitForReadyState();
    Collection localCollection = sMemoryCookieStorage.getValidCookies();
    if (!localCollection.isEmpty())
    {
      arrayOfCookieHolder = new CookieHolder[localCollection.size()];
      int i = 0;
      Iterator localIterator = localCollection.iterator();
      while (localIterator.hasNext())
      {
        CookieHolderWrapper localCookieHolderWrapper = (CookieHolderWrapper)localIterator.next();
        int j = i + 1;
        arrayOfCookieHolder[i] = localCookieHolderWrapper.getCookie();
        i = j;
      }
    }
    CookieHolder[] arrayOfCookieHolder = null;
    return arrayOfCookieHolder;
  }

  public static void initCookieManager(Context paramContext)
  {
    if (!sIsInitStarted)
    {
      sIsInitStarted = true;
      Thread localThread = new Thread(new Runnable()
      {
        public void run()
        {
          CookieManager.LOG.logDebug("CookieManager", "Starting CookieManager initialization process...", new Object[0]);
          CookieManager.sSQLiteCookieStorage = new SQLiteCookieStorage(this.val$applContext);
          CookieManager.sMemoryCookieStorage = new MemoryCookieStorage(CookieManager.sSQLiteCookieStorage.getValidCookies());
          CookieManager.sIsReady = true;
          CookieManager.LATCH.countDown();
          CookieManager.LOG.logDebug("CookieManager", "CookieManager initialization process has finished.", new Object[0]);
        }
      });
      localThread.setName("Cookie init thread.");
      localThread.start();
    }
  }

  static boolean isInitialized()
  {
    try
    {
      checkManager();
      waitForReadyState();
      bool = true;
      return bool;
    }
    catch (RuntimeException localRuntimeException)
    {
      while (true)
        boolean bool = false;
    }
    finally
    {
    }
  }

  public static void saveCookies(CookieHolder[] paramArrayOfCookieHolder)
  {
    checkManager();
    waitForReadyState();
    List localList;
    if ((paramArrayOfCookieHolder != null) && (paramArrayOfCookieHolder.length > 0))
      localList = Arrays.asList(paramArrayOfCookieHolder);
    try
    {
      sMemoryCookieStorage.processCookies(localList);
      DbSaveWorker localDbSaveWorker = new DbSaveWorker(localList, sSQLiteCookieStorage);
      SAVE_QUEUE.execute(localDbSaveWorker);
      return;
    }
    catch (Exception localException)
    {
      LoggerExt localLoggerExt = LOG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = localException.getMessage();
      localLoggerExt.logError("CookieManager", "Exception during saving new cookies: {0}", arrayOfObject);
    }
  }

  static void setMemoryCookieStorage(ICookieStorage paramICookieStorage)
  {
    try
    {
      checkManager();
      waitForReadyState();
      sMemoryCookieStorage = paramICookieStorage;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  static void setSQLiteCookieStorage(ICookieStorage paramICookieStorage)
  {
    try
    {
      checkManager();
      waitForReadyState();
      sSQLiteCookieStorage = paramICookieStorage;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private static void waitForReadyState()
  {
    try
    {
      while (!sIsReady)
        LATCH.await();
    }
    catch (InterruptedException localInterruptedException)
    {
      Thread.currentThread().interrupt();
      LOG.logWarning("CookieManager", "Thread was interrupted while waiting for manager initialization!", new Object[0]);
      return;
    }
    finally
    {
    }
  }

  private static class DbSaveWorker
    implements Runnable
  {
    private final List<CookieHolder> mNewCookies;
    private final ICookieStorage mStorage;

    DbSaveWorker(List<CookieHolder> paramList, ICookieStorage paramICookieStorage)
    {
      this.mNewCookies = paramList;
      this.mStorage = paramICookieStorage;
    }

    public void run()
    {
      Utils.lowerCurrentThreadPriority();
      try
      {
        this.mStorage.processCookies(this.mNewCookies);
        return;
      }
      catch (Exception localException)
      {
        LoggerExt localLoggerExt = CookieManager.LOG;
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = localException.getMessage();
        localLoggerExt.logError("CookieManager", "Exception during saving new cookies: {0}", arrayOfObject);
      }
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.network.beans.cookie.CookieManager
 * JD-Core Version:    0.6.2
 */